package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.IASeismicAssess;
import com.css.fxfzaqbz.modules.data.repository.IASeismicAssessRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class IASeismicAssessRepositoryImpl implements IASeismicAssessRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public IASeismicAssess saveIASeismicAssess(IASeismicAssess iaSeismicAssess) {
        String sql = "insert into sys_seismic_assess (" +
                "sa_id,sa_name,sa_ywry,sa_lxfs,sa_update_time,sa_czr," +
                "del_flag,create_user,create_time,update_user,update_time,ia_id" +
                ")values(" +
                ":saId,:saName,:saYwry,:saLxfs," +
                ":saUpdateTime,:saCzr,:delFlag,:createUser," +
                ":createTime,:updateUser,:updateTime,:iaId)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(iaSeismicAssess);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return iaSeismicAssess;
    }

    @Override
    public List<IASeismicAssess> getIASeismicAssesses(String id) {
        StringBuilder sql = new StringBuilder(" select a.* from sys_seismic_assess a ");
        StringBuilder whereSql = new StringBuilder(" where a.del_flag = '" + YNEnum.Y.toString() + "'");
        if (!PlatformObjectUtils.isEmpty(id)) {
            whereSql.append(" and a.ia_id = '" + id + "'");
        }
        String wholeSql = sql.append(whereSql).toString();
        return jdbcTemplate.query(wholeSql, new BeanPropertyRowMapper<>(IASeismicAssess.class));
    }

    @Override
    public IASeismicAssess getASeismicAssessById(String id) {
        String sql = "select * from sys_seismic_assess where sa_id = '" + id + "';";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(IASeismicAssess.class));
    }

    @Override
    public IASeismicAssess updateIASeismicAssess(IASeismicAssess iaSeismicAssess) {
        String sql = "update sys_seismic_assess set" +
                " sa_name = :saName, " +
                " sa_ywry = :saYwry," +
                " sa_lxfs = :saLxfs," +
                " sa_update_time = :saUpdateTime," +
                " sa_czr = :saCzr," +
                " ia_id = :iaId," +
                " del_flag = :delFlag," +
                " create_user = :createUser," +
                " create_time = :createTime," +
                " update_user = :updateUser, " +
                " update_time = :updateTime " +
                " where sa_id = '" + iaSeismicAssess.getSaId() + "'";


        SqlParameterSource source = new BeanPropertySqlParameterSource(iaSeismicAssess);
        namedParameterJdbcTemplate.update(sql, source);
        return iaSeismicAssess;
    }

    @Override
    public void delIASeismicAssess(List<String> ids, Timestamp timestamp, String userID) {
        String sql = "update sys_seismic_assess set del_flag = '" + YNEnum.Y + "' , update_time = '" + timestamp + "' ,update_user = '" + userID + "'" + " where sa_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<IASeismicAssess> getIASeismicAssessByTask(String iaId, String taskName) {
        StringBuilder sql = new StringBuilder(" select a.* from sys_seismic_assess a ");
        StringBuilder whereSql = new StringBuilder(" where a.del_flag = '" + YNEnum.Y.toString() + "'");
        if (!PlatformObjectUtils.isEmpty(iaId)) {
            whereSql.append(" and a.ia_id = '" + iaId + "'");
        }
        if (!PlatformObjectUtils.isEmpty(taskName)) {
            whereSql.append(" and a.sa_name = '" + taskName + "'");
        }
        String wholeSql = sql.append(whereSql).toString();
        System.out.println(wholeSql);
        return jdbcTemplate.query(wholeSql, new BeanPropertyRowMapper<>(IASeismicAssess.class));
    }
}
